/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare.plugins');
(function () {
	
	/**
	 * This plugin is used to bind the InventoryPanel to the events of the CompareVehiclesPanel
	 * This will keep the InventoryPanel always insync with the display of the CompareVehiclesPanel.
	 * @class InventoryCompareVehiclesConnector
	 * @extends Ext.util.Observable
	 * @namespace net.autodata.compare.plugins
	 */
	var Plugin = Ext.extend(Ext.util.Observable, {
		/**
		 * @property comparePanel
		 * @type net.autodata.compare.CompareVehiclesPanel
		 */
		comparePanel: undefined,
		
		/**
		 * @property inventoryPanel
		 * @type net.autodata.compare.InventoryPanel
		 */
		inventoryPanel: undefined,
		
		constructor: function (cfg)
		{
			cfg = cfg || {};
			
			Ext.apply(this, cfg);
			
			if (this.comparePanel === undefined) {
				throw 'comparePanel is a required parameter';
			}
			
			Plugin.superclass.constructor.apply(this, arguments);
		},
		
		/**
		 * @method init
		 * @param {net.autodata.compare.InventoryPanel} inventoryPanel
		 */
		init: function (inventoryPanel)
		{
			this.inventoryPanel = inventoryPanel;
			
			this.comparePanel.getStore().on({
				'load': this.onLoad,
				'updatecompetitor': this.onLoad,
				scope: this
			});
			
			this.inventoryPanel.on('destroy', this.onDestroy, this);
		},
		
		/**
		 * This method is connected to the CompareVehiclePanel's store to pass the data into the
		 * InventoryPanel's store
		 * @method onLoad
		 * @private
		 */
		onLoad: function ()
		{
			var data = [];
			var store = this.comparePanel.getStore();
			
			Ext.each(store.getRange(), function (record) {
				data.push(record.data);
			});
			
			this.inventoryPanel.getStore().loadData({rows: data});
		},
		
		/**
		 * Used to clean up all event bindings made by this plugin
		 * @private
		 */
		onDestroy: function ()
		{
			if (this.comparePanel) {
				this.comparePanel.getStore().un({
					'load': this.onLoad,
					'updatecompetitor': this.onLoad,
					scope: this
				})
			}
			
			this.inventoryPanel.on('destroy', this.onDestroy, this);
		}
		
	});
	
	net.autodata.compare.plugins.InventoryCompareVehiclesConnector = Plugin;
	
})();